<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="库id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inventoryId">
              <a-select
                @change="ckchange"
                v-model="model.inventoryId"
                :allowClear="true"
                style="width: 100%"
                placeholder="请选择所属仓库id">
                <a-select-option v-for="(role,roleindex) in warehouseList" :key="role.id" :value="role.id" >
                    {{ role.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemNumber">
              <a-select
                @change="spchange"
                v-model="model.itemNumber"
                :allowClear="true"
                style="width: 100%"
                placeholder="请选择商品编号">
                <a-select-option v-for="(role,roleindex) in itemList" :key="role.id" :value="role.id">
                    {{ role.itemNumber }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tradeName">
              <a-input v-model="model.tradeName" :disabled="isDisabled" placeholder="请输入商品名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="commoditySpecifications">
              <a-input v-model="model.commoditySpecifications" :disabled="isDisabled" placeholder="请输入商品规格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="原库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalInventoryQuantity">
              <a-input v-model="model.originalInventoryQuantity" :disabled="isDisabled" placeholder="请输入原库存数量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="盘点库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countTheInventoryQuantity">
              <a-input v-model="model.countTheInventoryQuantity" placeholder="请输入盘点库存数量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量变更原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reasonForQuantityChange">
              <a-input v-model="model.reasonForQuantityChange" placeholder="请输入数量变更原因"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ChTakeInventoryForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        isDisabled: true,
        model:{
          inventoryId:"",
         },
        itemList:{},
        warehouseList:{},
        item:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        queryParam: {
          
        },
        url: {
          add: "/ch/chTakeInventory/add",
          edit: "/ch/chTakeInventory/edit",
          queryById: "/ch/chTakeInventory/queryById",
          aselect:"/ch/chWarehouse/queryList",
          itemNumber:"/ch/chWarehouse/queryChWarehouseShopInfoList"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getWarehouseList();
    },
    methods: {
      spchange(e){
        console.log('e',e);
        console.log('itemList',this.itemList);
        for(var i = 0; i < this.itemList.length; i++){
            if(this.itemList[i].id == e){
              this.model.tradeName = this.itemList[i].name;
              this.model.commoditySpecifications = this.itemList[i].commoditySpecifica;
              this.model.originalInventoryQuantity = this.itemList[i].quantityOfGoods;
            }
        }
      },
      ckchange(e){
        console.log('e',e);
        delete this.model.itemNumber;
        delete this.model.tradeName;
        delete this.model.commoditySpecifications;
        delete this.model.originalInventoryQuantity;
        this.getitemNumber();
        
      },
      getitemNumber(){
        httpAction(this.url.itemNumber, { warehouseId: this.model.inventoryId }, 'post').then((res) => {
                if (res.success) {
                    console.log("add-res:", res);
                    this.itemList = [];
                    var obj = {};
                    var arr = res.result;
                    console.log(arr);
                    for(var i = 0;i<arr.length;i++){
                      var obj = {};
                      obj.id=arr[i].id;
                      obj.name=arr[i].tradeName;
                      obj.commoditySpecifica = arr[i].commoditySpecifica;
                      obj.quantityOfGoods=arr[i].quantityOfGoods;
                      obj.itemNumber = arr[i].itemNumber;
                      this.itemList.push(obj);
                    }
                } else {
                    this.$message.warning(res.message);
                }
            })
      },
      getWarehouseList(){
        getAction(this.url.aselect,this.model.inventoryId).then((res)=>{
          if(res.success){
            this.warehouseList = [];
            var obj = {};
            var arr = res.result;
            console.log(arr);
            for(var i = 0;i<arr.length;i++){
              var obj = {};
              obj.id=arr[i].id;
              obj.name=arr[i].remark;
              this.warehouseList.push(obj);
            }
            this.model.inventoryId = this.warehouseList[0].id;
            this.getitemNumber();
          }
          else{
            this.$message.info(res.message);
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>